#include "videothread.h"
#include <QDebug>
namespace wxb{
VideoThread::VideoThread(QObject *parent)
    : QThread{parent}
{}

void VideoThread::addPktToCache(AVPacket *pkt)
{
    pktList.push_back(pkt);
}

void VideoThread::run(){
    qDebug() << "void VideoThread::run()";

    while (!exitFlag)
    {
        //没有数据
        if (pktList.empty()){
            continue;
        }
        //拿出第一个节点数据
        AVPacket *pkt = pktList.front();
        pktList.pop_front();
        bool ret = decode.sendToDecoder(pkt);
        if (!ret)
        {
            continue;
        }
        while (true)
        {
            AVFrame * frame = decode.receiveFrame();
            if (frame == nullptr){
                break;
            }
            av_frame_free(&frame);
            //显示视频YUV420P
            // qDebug() << "video frame->linesize[0]" << frame->linesize[0];//Y 1920
            // qDebug() << "video frame->linesize[1]" << frame->linesize[1];//U 960
            // qDebug() << "video frame->linesize[2]" << frame->linesize[2];//V 960
        }
    }
}

bool VideoThread::openDecoder(AVCodecParameters *params)
{
    //需要手动释放params
    this->params = params;
    return decode.openDecoder(params);
}
}
